package com.museum.service;



import com.google.gson.Gson;
import com.museum.core.utils.RedisUtil;
import com.museum.core.utils.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: 王瑞文
 * Date: 2020/9/6
 * Time: 11:53
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SendSms {
    @Autowired
    private SendSmsUtil sendSmsUtil;
    @Autowired
    private RedisUtil redisUtil;


    public static class SmsData{
        String code;

        public SmsData(String code) {
            this.code = code;
        }
    }
    /**
     * 发送验证码
     * @param PhoneNumbers 手机号
     * @return
     */

    public boolean sendSms( String PhoneNumbers){
        //随机生成验证码
        String numeral = VerificationCode.getNumeral();
        System.out.println(numeral);
        SmsData smsData = new SmsData(numeral);
        Gson gson = new Gson();
        String code = gson.toJson(smsData);
        System.out.println(code+"code");
        String information = sendSmsUtil.aliSendSms(PhoneNumbers, code);
        if(information.equals("ok")){
            redisUtil.setString(PhoneNumbers,numeral);
            return true;
        }
        return false ;
    }

    /**
     * 验证
     * @param PhoneNumbers
     * @param code
     * @param
     * @return
     */

    public boolean yanSms(String PhoneNumbers, String code){

        String redisCode =  redisUtil.getString(PhoneNumbers);
        if(code.equals(redisCode)){
            redisUtil.delKey(PhoneNumbers);
            return true;
        }
        return false;

    }

}

